package com.fly.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class MSkillZuulApplication {
	/**
	 * 自定义拦截器工厂
	 * 
	 * @return
	 */
	@Bean
	public RequestTimeGatewayFilterFactory elapsedGatewayFilterFactory() {
		return new RequestTimeGatewayFilterFactory();
	}

	/**
	 * 自定义全局拦截器
	 * 
	 * @return
	 */
	@Bean
	public TokenFilter tokenFilter() {
		return new TokenFilter();
	}

	/**
	 * 根据Hostname进行限流
	 * 
	 * KeyResolver需要实现resolve方法，比如根据Hostname进行限流，则需要用hostAddress去判断。
	 * 实现完KeyResolver之后，需要将这个类的Bean注册到Ioc容器中。
	 */
	//@Bean
	public HostAddrKeyResolver hostAddrKeyResolver() {
		return new HostAddrKeyResolver();
	}
	/**
	 * 根据uri去限流
	 * @return
	 */
	@Bean
    public UriKeyResolver uriKeyResolver() {
        return new UriKeyResolver();
    }

	public static void main(String[] args) {
		SpringApplication.run(MSkillZuulApplication.class, args);
	}

//	@Bean
//	public RouteLocator myRoutes(RouteLocatorBuilder builder) {
//		return builder.routes()
//				// 正常路由
//				.route(p -> p.path("/hello/**").uri("http://localhost:8853"))
//				
//				//属于localhost:8853的熔断路由
//				.route(p -> p.path("/hello/**")
//						.filters(f -> f.hystrix(config -> config.setName("mycmd").setFallbackUri("forward:/fallback")))
//						.uri("http://localhost:8853"))
//				
//				.build();
//
//	}

	@GetMapping("/fallback")
	public String fallback() {
//		System.err.println("网关熔断");
		return "fallback";
	}
}
